package com.testin.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.testin.model.Quest;
import com.testin.model.Require;
import com.testin.model.User;
import com.testin.service.UserTestingService;
import com.testin.service.impl.UserTestingServiceImpl;
import com.testin.utils.Result;
import com.testin.utils.WriteMailModel;

/**
 * 用户测试新servlet
 * @author liuqiang
 *
 */
public class UserTestingServlet extends HttpServlet{


	private static final long serialVersionUID = -2131060533631810232L;
	private UserTestingService userTestingService =new UserTestingServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			
		try{
		request.setCharacterEncoding("UTF-8");	
		String uri=request.getParameter("uri");
		String uriType=request.getParameter("uriType");
		String uriDescription=request.getParameter("uriDescription");
		String scene=request.getParameter("scene");
		int person=request.getParameter("person")==null?0:Integer.parseInt(request.getParameter("person"));
		String gender=request.getParameter("gender");
		int ageMin=request.getParameter("ageMin")==null?0:Integer.parseInt(request.getParameter("ageMin"));
		int ageMax=request.getParameter("ageMax")==null?0:Integer.parseInt(request.getParameter("ageMax"));
		String revenue=request.getParameter("revenue");
		String usageTerm=request.getParameter("usageTerm");
		String otherRequire=request.getParameter("otherRequire");
		String platformType=request.getParameter("platformType");
		String name=request.getParameter("name");
		String tel=request.getParameter("tel");
		String im=request.getParameter("im");
		String email=request.getParameter("email");
		String company=request.getParameter("company");
		String addr=request.getParameter("addr");
		String domain=request.getParameter("domain");
		
		String[] taskList = request.getParameterValues("taskList[]");
		String[] questionList=request.getParameterValues("questionList[]");
				
		//先存user信息
		User user  = new User();
		user.setName(name);
		user.setPhone(tel);
		user.setCmpAddress(addr);
		user.setImCode(im);
		user.setCmpName(company);
		user.setCmpUrl(domain);
		user.setMail(email);
		user.setDateTime(new Date());
		
		Require require = new Require();
		require.setAppType(uriType);
		require.setAppUrl(uri);
		require.setAppTestScene(scene);
		require.setAppDesc(uriDescription);
		require.setpQuantity(person);
		require.setpSex(gender);
		require.setpAgeMin(ageMin);
		require.setpAgeMax(ageMax);
		require.setpRevenue(revenue);
		require.setpUseterm(usageTerm);
		require.setpRequireOther(otherRequire);
		require.setPlatformType(platformType);
		require.setDateTime(new Date());
		
		//1:测试场景任务
		List<Quest> questList = new ArrayList<Quest>();	
		
		if(taskList!=null){
			for (int i = 0; i < taskList.length; i++) {
				Quest quest =new Quest();
				quest.setType("1");
				quest.setDataInfo(taskList[i]);
				quest.setDateTime(new Date());
				questList.add(quest);
			}
		}
		// 2:录制视频后需要测试者回答的问题
		if(questionList!=null){
			for (int i = 0; i < questionList.length; i++) {
				Quest quest =new Quest();
				quest.setType("2");
				quest.setDataInfo(questionList[i]);
				quest.setDateTime(new Date());
				questList.add(quest);
			}
		}
		
		
		Result result =userTestingService.saveUserTestingInfo(user,require,questList);
		if(result!=null){
			if(result.isSuccess()){
				WriteMailModel writeMailModel = new WriteMailModel();
				writeMailModel.sendMail(user.getMail(),user,require,taskList,questionList);
			}
			response.getWriter().print(result.getMessages());
			
		}
		
		

		}catch(Exception e){
			e.printStackTrace();
		}
		// 得到当前用户然后注销
		/*if (session.getAttribute("curPsd") != null) {
			session.removeAttribute("curPsd");
			session.invalidate();
		}*/
		//response.sendRedirect(this.getServletContext().getContextPath()+"/login.jsp");
	}
	
}
